package com.unknowpupil.web.base.email.controller;

import com.unknowpupil.web.base.email.entity.Mail;
import com.unknowpupil.web.base.email.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


/**
 * @Classname: MailController
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/10/8 16:10
 * @Description:
 * @Version: 1.0
 */
@Controller
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;


    /**
     * 发送简单邮件
     *
     * @param mail
     * @return
     */
    @PostMapping("/simpleSend")
    @ResponseBody
    public String sendSimpleMail(Mail mail) {
        mail.setTo("1392224290@qq.com");
        mailService.sendSimpleMail(mail);
        return "发送成功";
    }


    /**
     * 带附件的文件
     * @param mail
     * @return
     */
    @PostMapping("/sendAttachmentsMail")
    @ResponseBody
    public String sendAttachmentsMail(Mail mail) {
        mail.setTo("1392224290@qq.com");
        mailService.sendAttachmentsMail(mail);
        return "发送成功";
    }

    /**
     * 模板邮件
     * 带html
     */
    @RequestMapping("/sendTemplateMail")
    @ResponseBody
    public String sendTemplateMail() {
        //创建邮件正文
        Context context = new Context();
        context.setVariable("miner", "w10g");
        context.setVariable("web", "文梦祥");
        context.setVariable("company", "测试公司");
        context.setVariable("product", "梦想产品");
        String emailContent = templateEngine.process("reg", context);
        Mail mail = new Mail();
        mail.setContent(emailContent);
        mail.setTo("1392224290@qq.com");
        mailService.sendHtmlMail(mail);
        return "sucess";
    }




}


